package algorithm_optimal_solution.LinkedList;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;

//删除无序链表中值重复出现的节点：用栈。或类似选择排序的过程，将每一个数后面的数全部删除，但是第二种花费时间O(n^2)，空间复杂度O(1)。
public class RemoveLinkedListRepeatNode {

    static class Node{
        private int value;
        private Node node;

        public Node(int value,Node node){
            this.value = value;
            this.node = node;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        public Node getNode() {
            return node;
        }

        public void setNode(Node node){
            this.node = node;
        }
    }

    public void myRemoveLinkedListRepeat(Node head){
        Set<Integer> set = new HashSet<>();
        Node node = head;
        Node pre = null;
        while(node != null){
            if(!set.contains(node.getValue())){
                set.add(node.getValue());
                if(pre == null){
                    pre = node;
                }else{
                    pre.setNode(node);
                    pre = node;
                }
            }
            node = node.getNode();
        }
        if(pre != null) {
            pre.setNode(null);
        }
        StringBuilder sb = new StringBuilder();
        while(head != null){
            sb.append(head.getValue()).append(" ");
            head = head.getNode();
        }
        System.out.println(sb);
    }

    public void removeLinkedListRepeatByStack(Node head){
        Set<Integer> set = new HashSet<>();
        Node node = head;
        Node pre = null;
        while(node != null){
            if(set.contains(node.getValue())){
                pre.setNode(node.getNode());
            }else{
                set.add(node.getValue());
                pre = node;
            }
            node = node.getNode();
        }
        StringBuilder sb = new StringBuilder();
        while(head != null){
            sb.append(head.getValue()).append(" ");
            head = head.getNode();
        }
        System.out.println(sb);
    }

    public void removeLinkedListBySelectionSort(Node head){
        Node node = head;
        Node pre = null;
        Node next = null;
        while(node != null){
            pre = node;//该值必然不是重复的
            next = node.getNode();
            while(next != null){
                if(node.getValue() == next.getValue()){
                    pre.setNode(next.getNode());
                }else{
                    pre = next;
                }
                next = next.getNode();
            }
            node = node.getNode();
        }
        StringBuilder sb = new StringBuilder();
        while(head != null){
            sb.append(head.getValue()).append(" ");
            head = head.getNode();
        }
        System.out.println(sb);
    }

    public static void main(String[] args)throws IOException {
        BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
        int len = Integer.parseInt(scanner.readLine());
        String[] numbers = scanner.readLine().split(" ");
        Node node = null;
        for(int i = len - 1;i >= 0;i--){
            node = new Node(Integer.parseInt(numbers[i]),node);
        }
//        new RemoveLinkedListRepeatNode().myRemoveLinkedListRepeat(node);
//        new RemoveLinkedListRepeatNode().removeLinkedListRepeatByStack(node);
        new RemoveLinkedListRepeatNode().removeLinkedListBySelectionSort(node);
    }
}
